﻿Public Class inscriptionEntreprise
    Dim dbEntreprises As New cls_taco
    Dim codeProvince As String
    Dim idEntreprise As Integer
    Dim messageerreur As String

    Private Sub inscriptionEntreprise_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        frmGestionEntreprises.btnRechercher.PerformClick()
        Me.Dispose()
    End Sub

    Public Overloads Function ShowDialog(ByRef v_idEntreprise) As Windows.Forms.DialogResult


        If v_idEntreprise <> Nothing Then
            idEntreprise = v_idEntreprise
            remplirinfo()
            btnAjouter.Text = "Enregistrer"
        Else
            remplirInfoParDefaut()
            btnAjouter.Text = "Ajouter"
        End If


        Return MyBase.ShowDialog()
    End Function

    Private Sub inscriptionEntreprise_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    End Sub

    Private Sub chkActif_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkActif.CheckedChanged

    End Sub

    Public Sub remplirinfo()
        'créé la connection à la base de données
        dbEntreprises = New cls_taco

        dbEntreprises.defDataset("SELECT noEntreprise,nom,telephone,adresse,CodeProvince,ville,codepostal,actif,commentaire from tblEntreprises where noEntreprise = " & idEntreprise & ";", "tblEntreprises")


        For Each colonne As DataColumn In dbEntreprises.ds.Tables("tblEntreprises").Columns

            With dbEntreprises.ds.Tables("tblEntreprises")
                Select Case colonne.ColumnName

                    Case "nom"
                        TxtNom.Text = nvl(.Rows(0).Item("nom"), "")
                    Case "adresse"
                        txtAdresse.Text = nvl(.Rows(0).Item("adresse"), "")
                    Case "codepostal"
                        txtCodePostal.Text = nvl(.Rows(0).Item("codepostal"), "")
                    Case "commentaire"
                        txtCommentaire.Text = nvl(.Rows(0).Item("commentaire"), "")
                    Case "CodeProvince"

                        codeProvince = nvl(.Rows(0).Item("CodeProvince"), "")
                        remplirListCbo("province", cboProvince.Text, cboProvince, "")
                        cboProvince.Text = getProvince(codeProvince)
                    Case "ville"
                        cboVille.Text = nvl(.Rows(0).Item("ville"), "")
                        remplirListCbo("ville", cboVille.Text, cboVille, " WHERE v.CodeProvince = p.CodeProvince AND p.nomProvince = '" & cboProvince.SelectedItem & "'")

                    Case "telephone"
                        txtTel.Text = nvl(.Rows(0).Item("Telephone"), "")


                    Case "actif"

                        If .Rows(0).Item("actif") = "True" Then
                            chkActif.Checked = True
                        Else
                            chkActif.Checked = False
                        End If

                End Select
            End With
        Next

    End Sub

    Public Sub remplirInfoParDefaut()
        remplirListCbo("province", "Choisissez une province", cboProvince, "")

        remplirListCbo("ville", "Choisissez une province", cboVille, " WHERE v.CodeProvince = p.CodeProvince AND p.nomProvince = '" & cboProvince.SelectedItem & "'")

    End Sub

    Private Sub cboProvince_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboProvince.SelectedIndexChanged

    End Sub

    Private Sub cboProvince_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboProvince.SelectionChangeCommitted
        remplirListCbo("ville", "Choisissez une ville", cboVille, " WHERE v.CodeProvince = p.CodeProvince AND p.nomProvince = '" & cboProvince.SelectedItem & "'")
    End Sub

    Public Function validerDonnee()
        Return validerTel(txtTel.Text, messageerreur, False) = True And validerNom(TxtNom.Text, messageerreur, False) _
        And validerNomAutre(TxtNom.Text, messageerreur, False) And validerAdresse(txtAdresse.Text, messageerreur, False) _
        And validerVille(cboVille.Text, messageerreur, False) And validerCodePostal(txtCodePostal.Text, messageerreur, True)



    End Function

    Private Sub btnAjouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAjouter.Click
        txtAdresse.Text = Trim(txtAdresse.Text)
        TxtNom.Text = Trim(TxtNom.Text)
        txtTel.Text = Trim(txtTel.Text)
        txtCodePostal.Text = Trim(txtCodePostal.Text)
        cboVille.Text = Trim(cboVille.Text)
        txtCommentaire.Text = Trim(txtCommentaire.Text)
        TxtNom.Text = StrConv(TxtNom.Text, VbStrConv.ProperCase)
        cboVille.Text = StrConv(cboVille.Text, VbStrConv.ProperCase)

        messageerreur = "Erreur: "
        If validerDonnee() Then

            If villeExiste(cboVille.Text) = False Then
                ajouterVilles(cboVille.Text, getCodeProv(cboProvince.Text))
            End If

            If btnAjouter.Text = "Enregistrer" Then
                For Each entreprise As DataRow In dbEntreprises.ds.Tables("tblEntreprises").Rows
                    With entreprise
                        .Item("CodePostal") = txtCodePostal.Text
                        .Item("CodeProvince") = getCodeProv(cboProvince.Text)
                        .Item("Nom") = TxtNom.Text
                        .Item("Adresse") = txtAdresse.Text
                        .Item("Ville") = cboVille.Text
                        .Item("Telephone") = txtTel.Text
                        .Item("Actif") = chkActif.Checked
                        .Item("commentaire") = txtCommentaire.Text
                    End With
                Next

                dbEntreprises.update("tblEntreprises")

            ElseIf btnAjouter.Text = "Ajouter" Then

                ajouterEntreprises(txtCodePostal.Text, getCodeProv(cboProvince.Text), TxtNom.Text, txtAdresse.Text, cboVille.Text, txtTel.Text, chkActif.Checked, txtCommentaire.Text)
                If InscriptionClients.IsHandleCreated = True Then
                    idEntreprise = getNoEntreprise(TxtNom.Text, txtAdresse.Text)
                    InscriptionClients.updateEntreprise(idEntreprise)
                    idEntreprise = Nothing
                    If frmGestionEntreprises.IsHandleCreated = True Then
                        frmGestionEntreprises.Close()
                    End If
                End If

            End If
            Me.Close()
        Else
            MsgBox(messageerreur)
        End If

    End Sub

    Private Sub btnAnnuler_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnnuler.Click
        idEntreprise = Nothing
        Me.Close()
    End Sub

    Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
End Class